package com.zx.deploy.common.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class GZipUtil {

	
	/**
	 * 压缩一个目录
	 * 
	 * @param dir
	 * @param out
	 * @param basedir
	 */
	public static void compressDirectory(File dir, ZipOutputStream out,
			String basedir) {
		if (!dir.exists()) {
			return;
		}
		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			/* 递归 */
			compressByType(files[i], out, basedir + dir.getName() + "/");
		}
	}
	
	
	/**
	 * 判断是目录还是文件，根据类型（文件/文件夹）执行不同的压缩方法
	 * 
	 * @param file
	 * @param out
	 * @param basedir
	 */
	public static void compressByType(File file, ZipOutputStream out,
			String basedir) {
		/* 判断是目录还是文件 */
		if (file.isDirectory()) {
		   try {
			   if(file.listFiles().length==0) {
					ZipEntry entry = new ZipEntry(basedir + file.getName()+"/");
					out.putNextEntry(entry);
			   }else {
				   compressDirectory(file, out, basedir);  
			   }
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			compressFile(file, out, basedir);
		}
	}
	
	
	/**
	 * 压缩一个文件
	 * 
	 * @param file
	 * @param out
	 * @param basedir
	 */
	public static void compressFile(File file, ZipOutputStream out,
			String basedir) {
		if (!file.exists()) {
			return;
		}
		try {
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
			ZipEntry entry = new ZipEntry(basedir + file.getName());
			out.putNextEntry(entry);
			int count;
			byte data[] = new byte[1444];
			while ((count = bis.read(data, 0, 1444)) != -1) {
				out.write(data, 0, count);
			}
			bis.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	
	 /** 
     * 向src文件添加header 
     * @param header 
     * @param srcPath 
     * @throws Exception 
     */  
    public static void appendFileHeader(byte[] header,String srcPath) throws Exception{  
        RandomAccessFile src = new RandomAccessFile(srcPath, "rw");  
        int srcLength = (int)src.length() ;  
        byte[] buff = new byte[srcLength];  
            src.read(buff , 0, srcLength);  
            src.seek(0);  
            src.write(header);  
            src.seek(header.length);  
            src.write(buff);  
            src.close();  
    }
    
    
    public static byte[] getFileHeader(String srcPath) throws Exception {
    	 RandomAccessFile src =  new RandomAccessFile(srcPath, "r");  
    	 byte[] head = new byte[172];
    	 src.seek(0);  
    	 src.read(head);
    	 src.close();
    	 return head;
    }
	
    
    public static void deleteHeadInfo(String srcPath,String gzFilePath2) throws Exception{  
        RandomAccessFile src = new RandomAccessFile(srcPath, "rw"); 
        RandomAccessFile out = new RandomAccessFile(gzFilePath2, "rw");  
        int srcLength = (int)src.length() ;  
        byte[] buff = new byte[srcLength-172]; 
        src.seek(172);
        src.read(buff); 
        //清空内容
        out.seek(0);
        out.write(buff);  
        out.close();
        src.close();  
    }
    
}
